package com.glodon.paas.account.web.util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.output.ByteArrayOutputStream;

import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.mortennobel.imagescaling.ResampleFilters;
import com.mortennobel.imagescaling.ResampleOp;

public class ImageUtil {
	/**
	 * 按给定最大宽和高等比 缩小图片
	 * 当maxWidth和maxHight都比原有值小的时候才会生成小文件，并返回小文件 否则返回null
	 * 
	 * @param source
	 * @param maxWidth 最大宽度,如果等于0则不考虑
	 * @param maxhight 最大高度,如果等于0则不考虑
	 * @param zoomIn   是否放大
	 * @throws IOException
	 */
	public static BufferedImage makeThumbnail(BufferedImage source,int maxWidth, int maxHight, boolean zoomIn) throws IOException {
		if(source == null){
			return null;
		}
		int width = source.getWidth();
		int height = source.getHeight();

		if (maxWidth <= 0) {
			maxWidth = width;
		}
		if (maxHight <= 0) {
			maxHight = height;
		}

		float widthRatio = (float) maxWidth / width;
		float hightRatio = (float) maxHight / height;

		float targetRatio = (widthRatio > hightRatio) ? hightRatio : widthRatio;
		
		if(targetRatio >= 1 && !zoomIn){
			return source;
		}

		BufferedImage targetThumbnail = scaleImage(source,
				(int) (targetRatio * width), (int) (targetRatio * height));

		return targetThumbnail;
	} 

	/**
	 * 将给定头像
	 * @param original
	 * @param width
	 * @param height
	 * @return
	 */
	public static  BufferedImage scaleImage(BufferedImage source, int width, int height) {
		if(source == null){
			return null;
		}
		ResampleOp filter = new ResampleOp(width, height);
		filter.setFilter(ResampleFilters.getBiCubicFilter());
		BufferedImage scaledSubImg = filter.filter(source, null);
		return scaledSubImg;
	}

	
	public static byte[] makeThumbnailByteArray(InputStream source,int maxWidth, int maxHight, boolean zoomIn) throws IOException {
		if(source == null){
			return null;
		}
		BufferedImage image = ImageIO.read(source);	 
		BufferedImage thumbnail =  makeThumbnail(image,maxWidth, maxHight,  zoomIn);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
	    ImageIO.write(thumbnail, "png", out); 
		return out.toByteArray();
	}
	
	public static byte[] makeThumbnailByteArray(byte[] source,int maxWidth, int maxHight, boolean zoomIn) throws IOException {
		if(source == null){
			return null;
		}
		ByteArrayInputStream in = new ByteArrayInputStream(source);
		BufferedImage sourceImage = ImageIO.read(in);
		
		BufferedImage thumbnail = makeThumbnail(sourceImage, maxWidth, maxHight, zoomIn);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
	    ImageIO.write(thumbnail, "png", out); 
		return out.toByteArray();
	}

	
	/**
	 * 截取图片
	 * 
	 * @param image	
	 * @param x		
	 * @param y
	 * @param width
	 * @param height
	 * @return
	 */
	public static byte[] cutting(byte[] source,int x, int y, int width, int height){
		if(source == null){
			return null;
		}
		
		if(x < 0 || y < 0 || width < 1 || height < 1){
			throw PaasServerExceptionFactory.validateException("image process error:x="+x+",y="+y+",w="+width+",h="+height, null);
		}
		
		 try { 
			ByteArrayInputStream input = new ByteArrayInputStream(source);
			BufferedImage src = ImageIO.read(input);
			
			ImageFilter cropFilter  = new CropImageFilter(x, y, width, height);  
			Image  img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(src.getSource(), cropFilter));  
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
			Graphics g = tag.getGraphics();
			g.drawImage(img, 0, 0, null);
			g.dispose();
			
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ImageIO.write(tag, "png", out); 
			return out.toByteArray();
		} catch (Exception e) {
			throw PaasServerExceptionFactory.systemException("image process error.", null);
		}
	}
	
	
	public static void main(String[] args) throws Exception {
		String r1 = "C:/bbb.jpg";
		String pt = "C:/tt.png";
//		// 裁剪
//		InputStream in = new FileInputStream(r1);		 
//		ByteArrayOutputStream bout = new ByteArrayOutputStream();
//		bout.write(in);
//		byte[] image = bout.toByteArray();
//		byte[] bytes = cutting(image,100,100,200,200);
//		
//		FileOutputStream fout = new FileOutputStream(pt);
//		fout.write(bytes);
		
		// 缩放
		InputStream in = new FileInputStream(r1);
		byte[] im = makeThumbnailByteArray(in, 3000, 3000,false);
		
		FileOutputStream fout = new FileOutputStream(pt);
		fout.write(im);
	}
	
	
}
